<?php
namespace app\system\controller;

use app\common\model\FormFieldModel;
use app\common\model\FormModel;
use think\Request;

class FormFieldController extends BaseController
{

    private $formModel;
    private $formFieldModel;
    private $formId;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->formId         = $this->request->param('form_id/d', 0);
        $this->formModel      = new FormModel();
        $this->formFieldModel = new FormFieldModel();
    }

    /**
     * 表单字段列表
     * @return mixed
     */
    public function index()
    {
        $fieldList = $this->formFieldModel->where('form_id', $this->formId)->select();
        $this->assign('fieldList', $fieldList);
        return $this->fetch();
    }

    /**
     * 获取字段类型
     */
    public function getFieldTpl()
    {
        $action    = $this->request->param('action');
        $fieldType = $this->request->param('fieldType');
        return $this->fetch("form_field/data/{$fieldType}/{$action}");
    }

    /**
     * 添加字段
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'FormFieldValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->formFieldModel->addData($data)) {
                $this->success('添加成功！', url('index', ['form_id' => $this->formId]));
            } else {
                $this->error($this->formFieldModel->getError());
            }
        }
        $validRule = $this->formFieldModel->getValidateRule();
        $this->assign('validRule', $validRule);
        return $this->fetch();
    }

    /**
     * 修改表单
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'FormFieldValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->formFieldModel->editData($data)) {
                $this->success('修改成功！', url('index', ['form_id' => $this->formId]));
            } else {
                $this->error($this->formFieldModel->getError());
            }
        }
        $fieldId   = $this->request->param('field_id');
        $validRule = $this->formFieldModel->getValidateRule();
        $info      = $this->formFieldModel->get($fieldId);
        $this->assign('info', $info);
        $setField = $this->fetch("form_field/data/" . $info['field_type'] . "/edit");
        $this->assign('setField', $setField);
        $this->assign('validRule', $validRule);
        return $this->fetch();
    }

    /**
     * 删除字段
     */
    public function del()
    {
        $fieldId = $this->request->param('field_id/d', 0);
        if ($this->formFieldModel->delData($fieldId)) {
            $this->success('删除成功！');
        } else {
            $this->error($this->formFieldModel->getError());
        }
    }

}
